<?php

namespace Front;

use Core\Constant;
use Core\Listener\Front\FrontModuleListener;
use Core\Session\AdminSessionHelper;
use Core\Session\FrontSessionHelper;
use Zend\EventManager\EventManagerInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\Literal;
use Zend\Mvc\Router\Http\Segment;

class Module  implements AutoloaderProviderInterface
{

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'invokables' => array( ),
            'factories' => array( 
            
            ),
            'abstract_factories' => array( ),
            'aliases' => array( ),
            'services' => array( ),
            'initializers' => array( ),
            'shared' => array(/* see Types of Services */),
        );
    }

    //for 'controllers' -> it automatically composed by mvc stack
    //no need to call by your hand ( get('...') );
    public function getControllerConfig()
    {
        return array(

        );
    }

    //for 'view_helpers' call in view by $this->nameViewHelperRegistered()->dosomething()
    public function getViewHelperConfig()
    {
        return array(

        );
    }
    //for 'controller_plugins'
    public function getControllerPluginConfig()
    {
        return array(

        );
    }

    public function onBootstrap(MvcEvent $e){
        $app = $e->getApplication();
        $event = $app->getEventManager()->getSharedManager();

        $frontModuleListener = new FrontModuleListener();
        $event->attach('Core\Controller\BaseController', Constant::$EVENT_NAME_FRONT_EXCEPTION,
                array($frontModuleListener, 'processException'), 10
            );
        $this->loadRoute($app->getEventManager());
    }

    private function loadRoute(EventManagerInterface $em){

        $em->attach(MvcEvent::EVENT_ROUTE,array($this, 'makeRoutes'), 100 );
    }

    public function makeRoutes(MvcEvent $e){

        // get the router
        $router = $e->getRouter();

        $routeTable = $e->getApplication()->getServiceManager()->get('Model\RouteTable');
        $routeTable->setLang(FrontSessionHelper::getCurrentLang());
        $allRoutes = $routeTable->getAllWithPageInfo();

        foreach($allRoutes as $rd){
            // create each route.
            $controller = $rd->module.'\\Controller\\'.ucfirst($rd->controller);
            $textRoute = '/'.$rd->route;

            $route = Segment::factory(array(
                'route' =>$textRoute,
                'defaults' => array(
                    'controller' => $controller,
                    'action' => $rd->action,
                    'pageId'=>$rd->page_id,

                )
            ));

            // add it to the router
            $router->addRoute($rd->name, $route);
        }
    }

}